import os,sys,math
import pygame
from pygame.locals import *

import code

class Shell(code.InteractiveInterpreter):
    def __init__(self,window=None,*a,**kw):
        code.InteractiveInterpreter.__init__(self,*a,**kw)
        self.window = window
    def write(self,data):
        print repr(data)
        sys.stderr.write('ERROR: '+data)

class MouseEventHandler:
    def handleMouseEvent(self,event):
        print 'MouseEventHandler',event

class KeyEventHandler:
    def handleKeyEvent(self,event):
        print 'KeyEventHandler',event

class EventHandler(MouseEventHandler,
                   KeyEventHandler):
    def handleEvent(self,event):
        print 'EventHandler',event


class ShellWindow(EventHandler):
    w,h = 300,200
    fontSize = 12
    fontType = 'couriernew'
    bg = [255,255,255]
    fg = [0,0,0]
    def __init__(self):
        size = self.w,self.h
        self.rect = Rect((0, 0), size)
        self.surface = Surface(size, pygame.SRCALPHA | pygame.HWSURFACE)

        self.text = ''

    def handleMouseEvent(self,event):
        pass

    def draw(self):
        font = self.getFont()

    _font = None
    def getFont(self):
        if self._font is None:
            self._font = pygame.font.SysFont(self.fontType,self.fontSize)
        return self._font

class CodeWindow(EventHandler):
    pass

class MediaApp:
    screenRect = Rect(0, 0, 800, 600)
    windowStyle = 0  #|FULLSCREEN
    fontSize = 12
    fontType = 'courier'
    def __init__(self):
        self.score = 0
        self.frameNumber = 0
        self.windowStyle = windowStyle
        
        self.shellwin = ShellWindow()
        self.codewin = CodeWindow()

        self.soundwin = SoundWindow()
        self.imagewin = ImageWindow()

        self.layout = {
            'outer border': 5,
            'left side': 0.7, # % of total width
            'shell' : {
                'height':0.7,XS
                }
            }

        self.focusWindow = self.shellwin

    def getScreenRect(self):
        return self.screenRect
    def quit(self):
        pass

    def dispatchKeyEvent(self,event):
        self.focusWindow.handleKeyEvent(event)
    def dispatchMouseEvent(self,event):
        self.focusWindow.handleMouseEvent(event)

    def run(self):
        # Initialize pygame
        pygame.init()
        if pygame.mixer and not pygame.mixer.get_init():
            print 'Warning, no sound'
            pygame.mixer = None
        self.bestDepth = pygame.display.mode_ok(
            self.screenRect.size,
            self.windowStyle,
            32,
            )
        screen = pygame.display.set_mode(
            self.screenRect.size,
            self.windowStyle,
            self.bestDepth,
            )

        all = pygame.sprite.RenderUpdates()

        background = pygame.Surface(self.screenRect.size)
        screen.blit(background, (0,0))
        pygame.display.flip()

        clock = pygame.time.Clock()

        done = False
        while not done:
            #get input
            self.frameNumber += 1
            for event in pygame.event.get():
                if (event.type == QUIT or (event.type == KEYDOWN and
                                           event.key == K_ESCAPE)):
                    self.quit()
                    done = True
                elif event.type == KEYDOWN:
                    print event.type,event.key
                elif event.type == MOUSEBUTTONDOWN:
                    print event.type,event.button,event.pos


            #draw the map
            #screen.blit(currentMap.image,currentMap.rect)
            # clear/erase the last drawn sprites
            all.clear(screen, background)

            #update all the sprites
            all.update()

            #draw the objects
            dirty = all.draw(screen)
            if dirty: print dirty
            pygame.display.update(dirty)

            pygame.display.flip()
            #cap the framerate
            clock.tick(20)

        #if pygame.mixer:
        #    pygame.mixer.music.fadeout(1000)
        #pygame.time.wait(1000)

        

if __name__=='__main__':
    #s = Shell()
    #s.runcode('1/0')
    app = MediaApp()
    app.run()
